import json
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from pymysql.converters import escape_string

from SQL_CRUD_Interface.CRUD import select_interface

class Movie_script:
    english = ''
    chinese = ''
    def __init__(self, eng, chi):
        self.english = eng
        self.chinese = chi

@csrf_exempt
def Get_movie_script(request):
    if request.method == "POST":
        data = json.loads(request.body)
        currentPage = data.get("currentPage")
        pageSize = data.get("pageSize")
        name = data.get("name")
        print(currentPage)
        sql = '      select `english`,`chinese` from `tb_movie_script`  where `movie_name` = "%s"  order by `id`  LIMIT  %d , %d     '  % ( name, int(currentPage), int(pageSize))
        aaa = select_interface("127.0.0.1", "root", "root", "study_english_database", sql)
        movie_script_list = []
        for ii in aaa:
            eng = ii[0]
            chi = ii[1]
            new_obj = Movie_script(eng, chi)
            movie_script_list.append(new_obj.__dict__)

        json_str = json.dumps(movie_script_list, default=lambda object: object.__dict__, sort_keys=True, indent=4,ensure_ascii=False)
        return HttpResponse(json_str)
